package com.bodacn.keypoint.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bodacn.keypoint.R;
import com.bodacn.keypoint.bean.ClothSizeBean;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ClothSizeLvAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<ClothSizeBean> list;
    private boolean allowChange = false;
    private DecimalFormat fnum;

    public ClothSizeLvAdapter(List<ClothSizeBean> list, Context context) {
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.list = list;
        fnum = new DecimalFormat("##0.0");
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHodler;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_cloth_size_top, parent, false);
            viewHodler = new ViewHolder(convertView);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHolder) convertView.getTag();
        }



        viewHodler.tvUp.setText("+"+list.get(position).getUp());
        viewHodler.tvDown.setText("-"+list.get(position).getDown());

        viewHodler.edRealSize.setTag(position);
        viewHodler.edRealSize.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int cp = (int) viewHodler.edRealSize.getTag();
                list.get(cp).setRealSize(s + "");
                System.out.println("edRealSize--:" + s.toString());


                if (!TextUtils.isEmpty(s.toString())&&!TextUtils.isEmpty(list.get(cp).getNormalSize())
                        &&!TextUtils.isEmpty(list.get(cp).getUp())&&!TextUtils.isEmpty(list.get(cp).getDown())){
                    double num=Double.parseDouble(s.toString())-Double.parseDouble(list.get(cp).getNormalSize());
                    Log.e("111","num:"+num+";down："+list.get(cp).getDown()+";up:"+list.get(cp).getUp());
                    if (num>=-Double.parseDouble(list.get(cp).getDown())&&num<=Double.parseDouble(list.get(cp).getUp())){
                        viewHodler.edRealSize.setBackground(mContext.getResources().getDrawable(R.drawable.btn_white_green_line));
                    }else {
                        viewHodler.edRealSize.setBackground(mContext.getResources().getDrawable(R.drawable.btn_white_red_line));
                    }
                }

            }
        });
        viewHodler.edRealSize.setText(list.get(position).getRealSize());

        viewHodler.tvNormalSize.setText(list.get(position).getNormalSize());

        viewHodler.tvPartName.setText(list.get(position).getPartName());

        viewHodler.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                allowChange = isChecked;
                if (isChecked) {
                    isEdit(true, viewHodler.edRealSize);
                    viewHodler.edRealSize.setBackground(mContext.getResources().getDrawable(R.drawable.btn_white_blue_line));
                } else {
                    isEdit(false, viewHodler.edRealSize);
                    viewHodler.edRealSize.setBackground(null);
                }
            }
        });
        viewHodler.ivAdd.setTag(position);
        viewHodler.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckResult(viewHodler.edRealSize.getText().toString(),
                        "add", viewHodler.edRealSize);
            }

        });

        viewHodler.ivPlus.setTag(position);
        viewHodler.ivPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckResult(viewHodler.edRealSize.getText().toString(),
                        "plus", viewHodler.edRealSize);
            }
        });

        return convertView;
    }

    private void CheckResult(String beforeSewH, String type, EditText tv) {
        if (!TextUtils.isEmpty(beforeSewH + "")) {
            Float bh = Float.parseFloat(beforeSewH);
            if (bh > 0.0f) {
                if (type.equals("add")) {
                    bh = bh + 0.5f;
                } else {
                    bh = bh - 0.5f;
                }
            }
            String last = fnum.format(bh);
            tv.setText(last);
        }
    }



    private void isEdit(boolean value, EditText editText) {

        if (value) {

            editText.setFocusable(true);

            editText.setFocusableInTouchMode(true);

            editText.setFilters(new InputFilter[]{new InputFilter() {

                @Override

                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

                    return null;

                }

            }});

        } else {

//设置不可获取焦点

            editText.setFocusable(false);

            editText.setFocusableInTouchMode(false);

//输入框无法输入新的内容

            editText.setFilters(new InputFilter[]{new InputFilter() {

                @Override

                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

                    return source.length() < 1 ? dest.subSequence(dstart, dend) : "";

                }

            }});

        }
    }

    class ViewHolder {
        @BindView(R.id.cb)
        CheckBox cb;
        @BindView(R.id.tv_PartName)
        TextView tvPartName;
        @BindView(R.id.tv_NormalSize)
        TextView tvNormalSize;
        @BindView(R.id.tv_up)
        TextView tvUp;
        @BindView(R.id.tv_down)
        TextView tvDown;
        @BindView(R.id.iv_add)
        ImageView ivAdd;
        @BindView(R.id.ed_realSize)
        EditText edRealSize;
        @BindView(R.id.iv_plus)
        ImageView ivPlus;
        @BindView(R.id.mylistitem)
        LinearLayout mylistitem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
